#include "createtaskdialog.h"
#include "ui_createtaskdialog.h"
#include "global_var.h"
#include "utils.h"

#include <QDir>
#include <QMessageBox>

CreateTaskDialog::CreateTaskDialog(Dashboard* dashboard, QWidget *parent) :
    QDialog(parent),
    ui(new Ui::CreateTaskDialog)
{
    ui->setupUi(this);
    this->dashboard = dashboard;
    init();
}

CreateTaskDialog::~CreateTaskDialog()
{
    delete ui;
}

void CreateTaskDialog::on_buttonBox_accepted()
{
    if(is_exist(ui->input_tname->text(), tasks)) {
        QMessageBox::information(this, tr("Error"), tr("该任务已存在"), QMessageBox::Yes);
        return;
    }

    QDir store = QDir(out_path);
    if(!store.exists(ui->input_tname->text())) {
        store.mkdir(ui->input_tname->text());
    }

    QString save_path = QString("%1").arg(ui->input_tname->text());
    Task new_task(tr("random"), ui->input_tname->text(), current_user.getId(), QDateTime::currentDateTime(), ui->end_time->dateTime(), save_path, ACTIVE);
    new_task.add_member(get_group_by_name(ui->cbx_target->currentText(), current_groups));
    tasks.push_back(new_task);
    current_tasks.push_back(new_task);

    QMessageBox::information(this, tr("Success"), tr("创建成功"), QMessageBox::Yes);
    this->dashboard->reload();
    this->close();
}

void CreateTaskDialog::init()
{
    ui->end_time->setMinimumDateTime(QDateTime::currentDateTime());

    for(int i=0; i < current_groups.size(); i++) {
        ui->cbx_target->addItem(current_groups[i].getGroup_name());
    }
}
